'use strict';
import React from 'react';
import {connect} from 'dva/index';
import {Card, Col, Drawer, Form, Row, Select, Statistic, Table} from 'antd';

import styles from '../../index.less';
import ViewTitle from '../../../components/ViewTitle';
import FormBase from '../../../components/Base/FormBase';
import notify from "../../../components/NotificationExtra";
import {FormField, SearchForm} from 'antd-extra';
import moment from 'moment';
import 'moment/locale/zh-cn';
import ModalSalesOrderDetail from "./../../sales/modal/ModalSalesOrderDetail";
import {exportExcel} from "../../../utils/excelUtils";

moment.locale('zh-cn');

const _ = require('lodash');
const {Option} = Select;
const table = 'SalesOrder';
const title = '订单';

@Form.create()
@connect(({
            user,
            global,
            customer,
            sales,
            dict,
            material,
            store
          }) => ({...user, ...global, ...dict, ...material, ...customer, ...sales, ...store}))
export default class XSalesReceiptDataFace extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    const {dispatch} = this.props;
    const payload = this.props.form.getFieldsValue();
    if (!payload.projectType) payload["projectType"] = {$ne: "出租"};
    dispatch && dispatch({
      type: 'global/getModel',
      payload: {table}
    });


    dispatch && dispatch({
      type: 'dict/findMany',
      payload: {type: 'division'}
    });
    this.props.dispatch({
      type: 'customer/findMany',
      payload: {}
    });
    this.props.dispatch({
      type: 'material/findMany',
      payload: {}
    });
    payload.reviewStatus = "同意";
    this.props.dispatch({
      type: 'sales/findMany',
      payload
    });
  }

  getTableCols = () => {
    const model = this.props[`${table}Model`];
    const temp = this.getTableColumns(model);
    _.remove(temp, item => item.dataIndex === 'rentStartDate');
    _.remove(temp, item => item.dataIndex === 'rentEndDate');
    _.remove(temp, item => item.dataIndex === 'rentReturnDate');
    temp.push(
      {
        title: '操作', dataIndex: 'operation', key: 'operation',
        width: 160, fixed: 'right',
        render: (value, record) => {
          return (
            <span>
           {<a style={{marginRight: '6px'}}
               onClick={() => this.setState({showDetail: true, record})}>详情</a>}

            </span>
          );
        }
      });
    return temp;

  };

  getSearchFields = () => {
    const model = this.props[`${table}Model`];
    if (!model) return [];
    const arr = [];
    this.getSearchField(arr, model, "");
    const {prods} = model;

    this.getSearchField(arr, prods, "prods.");
    const {materials} = prods;
    this.getSearchField(arr, materials, "prods.materials.");
    arr.push(<FormField
      label={"是否开票"}
      name={"isKP"}
      key={"isKP"}
    >
      <Select allowClear>{[<Option value={"已开票"}>{"已开票"}</Option>,
        <Option value={"未开票"}>{"未开票"}</Option>]}</Select>
    </FormField>);
    arr.push(<FormField
      label={"是否发货"}
      name={"isFH"}
      key={"isFH"}
    >
      <Select allowClear>{[<Option value={"已发货"}>{"已发货"}</Option>,
        <Option value={"未发货"}>{"未发货"}</Option>]}</Select>
    </FormField>);
    return arr;
  };


  export = () => {
    this.props.dispatch({
      type: 'sales/exportExcel',
      payload: this.getSearchPayload()
    }).then(res => {
      if (res) {
        const {result = {}} = res;
        const {header, data} = result;
        exportExcel(header, data, '订单');
      }
    });
  };

  render() {
    const {sales = {}, _notify, form} = this.props;
    const {record = {}, showDetail} = this.state;

    if (_notify && this.state._notify !== _notify) {
      notify(_notify);
      this.state._notify = _notify;
    }

    const {amount, count, array} = sales;
    return (
      <Form className={styles.content}>
        <ViewTitle
          title={`${title}列表`}
          level={1}
          ></ViewTitle>
        <SearchForm
          style={{marginLeft: '6px', marginRight: '6px'}}
          onSearch={() => {
            this.state.current = 1;
            this.componentDidMount();
          }}
          onReset={this.handleReset}
          cols={4}
          min={8}
          expandText="展开"
          form={form}
        >
          {this.getSearchFields()}
        </SearchForm>
        <div style={{background: '#ECECEC', padding: '30px'}}>
          <Row gutter={16}>
            <Col span={10}>
              <Card>
                <Statistic
                  title="订单商品总数量"
                  value={count}
                  precision={2}
                  valueStyle={{color: '#3f8600'}}
                  suffix="KG"
                />
              </Card>
            </Col>
            <Col span={10}>
              <Card>
                <Statistic
                  title="订单商品总金额"
                  value={amount}
                  precision={2}
                  valueStyle={{color: '#cf1322'}}
                  suffix="元"
                />
              </Card>
            </Col>

          </Row>
        </div>
        <Table
          className={styles.antTable}
          bordered={true}
          columns={this.getTableCols()}
          dataSource={array}
          scroll={{x: true}}
          pagination={{
            current: this.state.current,
            total: this.state.total,
            showSizeChanger: true,
            onChange: page => {
              this.state.current = page;
              this.componentDidMount();
            },
            onShowSizeChange: (current, pageSize) => {
              this.state.current = 1;
              this.state.pageSize = pageSize;
              this.componentDidMount();
            }
          }}
          //  scroll={{ x: 1300 }}
        ></Table>

        {showDetail && <Drawer
          visible={showDetail}
          title={`${title}详情`}
          placement="left"
          width={'80%'}
          style={{padding: 0}}
          // maskClosable={false}
          onClose={() => {
            this.setState({showDetail: false});
            this.componentDidMount();
          }}
        >
          <ModalSalesOrderDetail
            values={record}
            treeId={this.props.treeId}
            title={this.props.title}
            isFinance={true}
          > </ModalSalesOrderDetail>
        </Drawer>}

      </Form>
    );
  }
}
